package com.jason.bbs.block.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Min;

import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jason.bbs.bean.ResultJson;
import com.jason.bbs.bean.block.Block;
import com.jason.bbs.block.service.BlockService;

@RestController
public class BlockController {
	@Resource
	private BlockService blockService;

	@PutMapping("/add")
	public ResultJson addBlock(String name) {
		ResultJson code = new ResultJson(200, "成功");
		if (StringUtils.isEmpty(name)) {
			code.setCode(400);
			code.setMessage("版块名称不能为空");
			return code;
		}
		if (blockService.exitis(name)) {
			code.setCode(400);
			code.setMessage("版块[" + name + "]已存在");
			return code;
		}
		Block block = blockService.add(name);
		code.put("blockId", block.getId());
		return code;
	}

	@DeleteMapping("/delete/{id}")
	public ResultJson delete(@PathVariable("id") String id) {
		ResultJson code = new ResultJson(200, "成功");
		if (StringUtils.isEmpty(id)) {
			code.setCode(400);
			code.setMessage("版块ID不能为空");
			return code;
		} else if (!id.matches("\\d+")) {
			code.setCode(400);
			code.setMessage("非法的ID:" + id);
			return code;
		}
		blockService.delete(id);
		return code;
	}

	@GetMapping("/find")
	public ResultJson find(@Valid Block block,
			@RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize,
			@RequestParam(name = "pageNumber", required = false, defaultValue = "1") int pageNumber, BindingResult br) {
		ResultJson code = new ResultJson(200, "成功");
		if (br.hasErrors()) {
			code.setCode(400);
			List<ObjectError> list = br.getAllErrors();
			String str = "";
			for (ObjectError objectError : list) {
				str += objectError.getDefaultMessage() + ",";
			}
			code.setMessage(str);
			return code;
		}
		Page<Block> page = blockService.find(block, pageSize, pageNumber - 1);
		// pageNumber 页码 1 1
		code.put("pageNumber", page.getNumber() + 1);
		// pageSize 每页数据量 10 10
		code.put("pageSize", page.getSize());
		// total 总数据量 100
		code.put("total", page.getTotalElements());
		// totalPage 总页数 10 10
		code.put("totalPage", page.getTotalPages());
		// list 查询结果 []
		code.put("list", page.getContent());
		return code;
	}

	@GetMapping("/exitis/{blockId}")
	public ResultJson exitis(@PathVariable("blockId") String blockId) {
		ResultJson code = new ResultJson(200, "成功");
		if (!blockId.matches("\\d+")) {
			code.setCode(400);
			code.setMessage("非法的版块编号:" + blockId);
			return code;
		}
		code.put("exitis", blockService.exitisById(blockId));
		return code;
	}
}
